package com.ly.hotel.spainterface.pp.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.jxd.bean.MachineBean;
import com.ly.hotel.spainterface.pp.bean.PpJSPluginBean;
import com.ly.hotel.spainterface.pp.bean.PreReturnBean;
import com.ly.hotel.spainterface.pp.utils.CommonHttpUtils;
import com.ly.hotel.spainterface.pp.utils.GsonUtils;
import com.ly.hotel.util.ProcessWaitingUtil;

import java.lang.ref.WeakReference;
import java.net.URLEncoder;

/**
 * Created by mo on 2018/6/1.
 */

public class PpArrangeDialog extends Dialog {

    private Context context;
    private final String TAG = "PPArrangeConfirm";
    private final int ARRANGE_ITEM = 0;
    private final int ARRANGE_ITEM_CALL = 1;
    private ImageButton dialogClose;
    private TextView itemNameText;
    private TextView locationTypeText;
    private TextView locationNoText;
    private Button confirmArrange;
    private PpJSPluginBean ppJSPluginBean;
    private MachineBean machineBean;
    private String itemName;
    private MyHandler handler = new MyHandler(this);

    public PpArrangeDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pp_arrange_confirm_dialog);
        initView();
        setData();
        setDialogClose();
        setConfirmArrange();
    }

    private void initView() {
        dialogClose = findViewById(R.id.dialog_close);
        itemNameText = findViewById(R.id.item_name_text);
        locationTypeText = findViewById(R.id.location_type_text);
        locationNoText = findViewById(R.id.location_no_text);
        confirmArrange = findViewById(R.id.confirm_arrange);
    }

    private void setData() {
        itemNameText.setText(itemName);
        locationTypeText.setText(machineBean.getCategoryName());
        locationNoText.setText(machineBean.getTvName());
    }

    private void setDialogClose() {
        dialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void setConfirmArrange() {
        confirmArrange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    ProcessWaitingUtil.showProcessDialog(context, context.getResources().getString(R.string.loading_tips), 10000);
                    if (TextUtils.isEmpty(ppJSPluginBean.getPrintPort())) {
                        getDisplay();
                    } else {
                        /*if (ppJSPluginBean.isDisplayPrint() == true) {
                            getDisplay();
                            getPrint();
                        } else {
                            getPrint();
                        }*/
                        getPrint();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void getDisplay() {
        try {
            String URL = "http://" + ppJSPluginBean.getIp() + ":" + ppJSPluginBean.getPort() + "/api/call.json";
            String request = "mac=" + machineBean.getMac() + "&content=" + URLEncoder.encode("项目:" + machineBean.getCategoryName() + "-" +
                    machineBean.getTvName() + "," + itemName, "utf-8") + "&status=0";
            Log.i(TAG, "request=" + request);
            CommonHttpUtils.getResource(URL, request, handler, ARRANGE_ITEM_CALL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getPrint() {
        try {
            String URL = "http://" + ppJSPluginBean.getIp() + ":" + ppJSPluginBean.getPrintPort() + "/call.json";
            String request = "fun=on&content=" + URLEncoder.encode("项目:" + machineBean.getCategoryName() + "-" +
                    machineBean.getTvName() + "," + itemName, "utf-8");
            Log.i(TAG, "request=" + request);
            CommonHttpUtils.getResource(URL, request, handler, ARRANGE_ITEM);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MyHandler extends Handler {
        private WeakReference<PpArrangeDialog> wrDialog;

        public MyHandler(PpArrangeDialog ppArrangeDialog) {
            wrDialog = new WeakReference<PpArrangeDialog>(ppArrangeDialog);
        }

        @Override
        public void handleMessage(Message msg) {
            PpArrangeDialog mDialog = wrDialog.get();
            if (mDialog != null) {
                switch (msg.what) {
                    case 1:
                        ProcessWaitingUtil.closeProcessDialog();
                        String resultString = msg.obj.toString();
                        if (resultString.equals("OK")) {
                            CommentCustomToast.makeText(context, "安排项目成功！", Toast.LENGTH_SHORT, true).show();
                        } else {
                            PreReturnBean preReturnBean = GsonUtils.parseSimpleJson(resultString, PreReturnBean.class);
                            if (preReturnBean != null) {
                                if (preReturnBean.getCode() == 200) {
                                    CommentCustomToast.makeText(context, "安排项目成功！", Toast.LENGTH_SHORT, true).show();
                                } else {
                                    CommentCustomToast.makeText(context, "安排项目失败！", Toast.LENGTH_SHORT, true).show();
                                }
                            } else {
                                CommentCustomToast.makeText(context, "安排项目失败！", Toast.LENGTH_SHORT, true).show();
                            }
                        }
                        dismiss();
                        break;

                    case 2:
                        ProcessWaitingUtil.closeProcessDialog();
                        CommentCustomToast.makeText(context, "安排项目失败！", Toast.LENGTH_SHORT, false).show();
                        dismiss();
                        break;
                }
            }
        }
    }

    ;

    public void setPpJSPluginBean(PpJSPluginBean ppJSPluginBean) {
        this.ppJSPluginBean = ppJSPluginBean;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public void setMachineBean(MachineBean machineBean) {
        this.machineBean = machineBean;
    }
}
